package cn.itcast.item.service.impl;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.item.ItemDoc;
import cn.itcast.hmall.pojo.user.User;
import cn.itcast.item.mqconstans.MqConstants;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.itcast.item.service.ItemService;
import cn.itcast.item.mapper.ItemMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
* @author 自己
* @description 针对表【tb_item(商品表)】的数据库操作Service实现
* @createDate 2022-06-21 10:33:46
*/
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item>
    implements ItemService{
@Autowired
ItemMapper itemMapper;
    @Autowired
    ItemService itemService;
    public Item findById(Long id){
        return itemMapper.selectById(id);

    }

    @Override
    public void stock(Long itemId, Integer num) {
        Item item = itemMapper.selectById(itemId);
        item.setStock(item.getStock()-num);
        itemService.updateById(item);

    }

    @Override
    public PageDTO<Item> find(SearchItemDTO searchItemDTO) {
        IPage<Item> page = new Page(searchItemDTO.getPage(),searchItemDTO.getSize());
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(searchItemDTO.getName())){
            wrapper.like("name","%"+searchItemDTO.getName()+"%");
        }
        if (StringUtils.isNotBlank(searchItemDTO.getBrand())){
            wrapper.eq("brand",searchItemDTO.getBrand());
        }
        if (StringUtils.isNotBlank(searchItemDTO.getCategory())){
            wrapper.eq("category",searchItemDTO.getCategory());
        }
        if (searchItemDTO.getBeginDate()!=null&&searchItemDTO.getEndDate()!=null){
            wrapper.between("create_time",searchItemDTO.getBeginDate(),searchItemDTO.getEndDate());
        }
        itemMapper.selectPage(page,wrapper);
        List<Item> records = page.getRecords();//当前页数据
        long total = page.getTotal();//总条数
        PageDTO<Item> pageDTO = new PageDTO<>(total,records);
        System.out.println(pageDTO);
        System.out.println(total);
        return pageDTO;
    }


    @Override
    public ResultDTO add(Item entity) {
        boolean save = itemService.save(entity);
        if (save==true){
            return ResultDTO.ok();
        }
        return ResultDTO.error();
    }

    @Override
    public ResultDTO updateStatus(String id, String status) {
        Item item = new Item();
        item.setStatus(Integer.parseInt(status));
        item.setId(Long.parseLong(id));
        boolean b = itemService.updateById(item);
        if (b==true&&item.getStatus()==1){
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_UP_KEY,item.getId());
            System.out.println("成功"+item.getId());
            return ResultDTO.ok();
        }
        if (b==true&&item.getStatus()==2){
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DOWN_KEY,item.getId());
            System.out.println("成功"+item.getId());
            return ResultDTO.ok();
        }
        return ResultDTO.error();
    }
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public ResultDTO update(Item item) {
        boolean b = itemService.updateById(item);
        if (b==true){

            return ResultDTO.ok();
        }
        return ResultDTO.error();
    }

    @Override
    public ResultDTO delete(Long id) {
        boolean b = itemService.removeById(id);
        if (b==true){
            return ResultDTO.ok();
        }
        return ResultDTO.error();
    }
}




